package us.kg.kotoriforest.schedule.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import us.kg.kotoriforest.schedule.common.Result;
import us.kg.kotoriforest.schedule.common.ResultCodeEnum;
import us.kg.kotoriforest.schedule.pojo.SysUser;
import us.kg.kotoriforest.schedule.service.Impl.SysUserServiceImpl;
import us.kg.kotoriforest.schedule.service.SysUserService;
import us.kg.kotoriforest.schedule.util.WebUtil;

import java.io.IOException;

/**
 * 接收与 sys_user 表有关业务的请求，并作出响应
 *
 * @author Kotori rotroutine@163.com
 * @since 2024/12/3
 */
@WebServlet("/user/*")
public class SysUserController extends BaseController {
    private static final SysUserService userService = new SysUserServiceImpl();
    private static final int SECONDS_OF_DAY = 86400; // 单位是s

    /**
     * 接收用户的注册请求，完成注册业务后返回响应的业务接口
     *
     * @param req  用户请求
     * @param resp 服务端响应
     * @throws ServletException ServletException
     * @throws IOException      IOException
     */
    private void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String userPwd = req.getParameter("userPwd");

        // 调用服务层方法，注册一个新用户
        SysUserService.State state = userService.register(new SysUser(null, username, userPwd));

        // 判断是否注册成功
        if (state == SysUserService.State.QUERY_SUCCESS) { // 注册用户成功
            resp.sendRedirect("/registerSuccess.html");
        } else { // 注册用户失败
            resp.sendRedirect("/registerFail.html");
        }
    }

    /**
     * 接收用户的登录请求，完成登录业务后返回响应的业务接口
     *
     * @param req  用户请求
     * @param resp 服务端响应
     * @throws ServletException ServletException
     * @throws IOException      IOException
     */
    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String userPwd = req.getParameter("userPwd");

        // 调用服务层代码进行登录验证
        SysUser loginUser = new SysUser(null, username, userPwd);
        SysUserService.State state = userService.login(loginUser);
        System.out.println(loginUser);

        // 判断是否验证成功
        if (state == SysUserService.State.USER_NOT_FOUND) {
            resp.sendRedirect("/loginUsernameError.html");
        } else if (state == SysUserService.State.QUERY_FAIL) {
            resp.sendRedirect("/loginUserPwdError.html");
        } else {
            // 登录成功则把该用户成功登录的状态放进 session 中
            HttpSession session = req.getSession();
            session.setMaxInactiveInterval(SECONDS_OF_DAY);
            session.setAttribute("sysUser", loginUser);
//            System.out.println(session.getId());
            resp.sendRedirect("/showSchedule.html");
        }
    }

    /**
     * 接收前端的用户名校验请求，完成校验并返回响应
     *
     * @param req  用户请求
     * @param resp 服务端响应
     * @throws ServletException ServletException
     * @throws IOException      IOException
     */
    private void checkUsernameUsed(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
//        String info = SysUserService.State.USER_NOT_FOUND == userService.checkUsernameUsed(username) ? "OK" : "NO";
//
//        resp.setContentType("text/html;charset=utf-8");
//        resp.getWriter().print(info);

        SysUserService.State state = userService.checkUsernameUsed(username);

        Result<Object> result = Result.ok(null);
        if (SysUserService.State.USER_ALREADY_EXIST == state) {
            result = Result.build(null, ResultCodeEnum.USERNAME_USED);
        }

        WebUtil.respondJSON(resp, result);
    }
}
